//Documentation complete
/**
 * @author Alex
 * @version 2009MR18
 */

package ui.internal.ajax;

import static problemDomain.ErrorBean.*;
import static utilities.XMLTools.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import problemDomain.ErrorBean;
import problemDomain.MenuBean;
import problemDomain.MenuItemBean;
import control.MenuControl;
import control.MenuItemControl;

/**
 * Servlet implementation class MaintainMenu
 */
public class MaintainMenu extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = 6877710503786346628L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml");
		HttpSession session = request.getSession(true);
		if (session != null) {
			String action = request.getParameter("a");
			if (action.equalsIgnoreCase("c"))
				create(request, response);
			else if (action.equalsIgnoreCase("r"))
				report(request, response);
			else if (action.equalsIgnoreCase("u"))
				update(request, response);
			else if (action.equalsIgnoreCase("d"))
				delete(request, response);
			else if (action.equalsIgnoreCase("l"))
				listAll(response);
			else if (action.equalsIgnoreCase("im"))
				listItemInMenu(request, response);
			else if (action.equalsIgnoreCase("mi"))
				listMenuItem(response);
			else if (action.equalsIgnoreCase("ai"))
				addItem(request, response);
			else if (action.equalsIgnoreCase("ri"))
				removeItem(request, response);
			else if (action.equalsIgnoreCase("si"))
				searchItem(request, response);
		}		
	}
	
	/* END (HttpServlet) Methods */
	
	
	/* Local Methods */
	
	private void create (HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		String description = request.getParameter("description");
		String active = request.getParameter("active");
		try {
			MenuControl menuCtrl = new MenuControl();
			menuCtrl.create(name, description, active);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}

	private void report (HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String active = request.getParameter("active");
		try {
			MenuControl menuCtrl = new MenuControl();
			List<MenuBean> list = menuCtrl.search(id, name, active);
			out.write(encapsulateBeanListXML(list, "menus"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
		
	}
	
	private void update (HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String description = request.getParameter("description");
		String active = request.getParameter("active");
		try {
			MenuControl menuCtrl = new MenuControl();
			menuCtrl.update(id, name, description, active);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		try {
			MenuControl menuCtrl = new MenuControl();
			menuCtrl.delete(id);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listAll(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			MenuControl menuCtrl = new MenuControl();
			List<MenuBean> list = menuCtrl.listAll();
			out.write(encapsulateBeanListXML(list, "menus"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listItemInMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			MenuControl menuCtrl = new MenuControl();
			List<MenuItemBean> list = menuCtrl.listItemInMenu(id);
				System.out.println(list.size());
			out.write(encapsulateBeanListXML(list,"menuitems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listMenuItem(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			MenuControl menuCtrl = new MenuControl();
			List<MenuItemBean> list = menuCtrl.listMenuItem();
			out.write(encapsulateBeanListXML(list,"menuitems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void addItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String menuID = request.getParameter("menuid");
		String menuItemID = request.getParameter("menuitemid");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			MenuControl menuCtrl = new MenuControl();
			menuCtrl.addItem(menuID, menuItemID);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void removeItem (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String menuItemID = request.getParameter("menuitemid");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			MenuControl menuCtrl = new MenuControl();
			menuCtrl.removeItem(menuItemID);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void searchItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		String number = request.getParameter("number");
		try {
			MenuItemControl miCtrl = new MenuItemControl();
			List<MenuItemBean> list = miCtrl.search(Integer.toString(0), number, name, Integer.toString(0));
			out.write(encapsulateBeanListXML(list, "menuitems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
		
	}
	
	/* END Local Methods */

}
